package dao;

import java.sql.SQLException;

import entity.User;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DataAccess {
	SQLiteDatabase db;
	private MySQLiteHelper dbHelper;
	private String[] allColumns = { 
			MySQLiteHelper.COLUMN_ID,
		    MySQLiteHelper.COLUMN_EMAIL,
		    MySQLiteHelper.COLUMN_Phone,
		    MySQLiteHelper.COLUMN_Address,
		    MySQLiteHelper.COLUMN_Sex,
		    MySQLiteHelper.COLUMN_Accesstoken,
		    MySQLiteHelper.COLUMN_inuse,
		    MySQLiteHelper.COLUMN_avatar
		    };
public DataAccess(Context context){
	dbHelper=new MySQLiteHelper(context);
	
	 /*
	 db=SQLiteDatabase.openDatabase("", null, SQLiteDatabase.CREATE_IF_NECESSARY);
	 String sql="CREATE TABLE IF NOT EXISTS user(id INTEGER primary key,email TEXT,phone TEXT,address TEXT,sex TEXT,accesstoken TEXT,inuse INT)";
		db.execSQL(sql);*/
 }
public void open() throws SQLException {
	db = dbHelper.getWritableDatabase();
	  }

public void close() {
	  dbHelper.close();
	  }
/*public void createTableUser(){
	String sql="CREATE TABLE IF NOT EXISTS user(id INTEGER primary key,email TEXT,phone TEXT,address TEXT,sex TEXT,accesstoken TEXT,inuse INT,avatar TEXT)";
	db.execSQL(sql);
}*/
public boolean insertUser(User user){
	ContentValues values=new ContentValues();
	values.put("id", user.getId());
	values.put("email",user.getEmail() );
	values.put("phone", user.getPhone());
	values.put("address", user.getAddress());
	values.put("sex", user.getSex());
	values.put("accesstoken", user.getAccessToken());
	values.put("inuse", user.getInUse());
	values.put("avatar", user.getAvatar());
	long i=db.insert("user", null, values);
	Log.d("insert return",user.getId()+"/"+user.getEmail()+"/"+ i+"");
	if(i==-1)return false;
	else return true;
	
}
public boolean updateUser(User u){
	ContentValues values=new ContentValues();
	values.put("email",u.getEmail() );
	values.put("phone", u.getPhone());
	values.put("address", u.getAddress());
	values.put("sex", u.getSex());
	values.put("accesstoken", u.getAccessToken());
	values.put("inuse", u.getInUse());
	values.put("avatar", u.getAvatar());
	String[] where=new String[1];
	where[0]=u.getId()+"";
	long i=db.update("user", values, "id=?", where);
System.out.println("db:"+u.getAvatar());
	if(i==0)return false;
	else return true;
}
public boolean removeInUse(int id){
	ContentValues values=new ContentValues();
	values.put("inuse", 0);
	values.put("accesstoken", "");
	String[] where=new String[1];
	where[0]=id+"";
	long i=db.update("user", values, "id !='"+id+"'", null);
	if(i==0)return false;
	else return true;
}
public boolean signOut(int id){
	ContentValues values=new ContentValues();
	values.put("inuse", 0);
	values.put("accesstoken", "");
	String[] where=new String[1];
	where[0]=id+"";
	long i=db.update("user", values, "id ='"+id+"'", null);
	if(i==0)return false;
	else return true;
}
public boolean signOut(){
	ContentValues values=new ContentValues();
	values.put("inuse", 0);
	values.put("accesstoken", "");
	String[] where=new String[1];
	long i=db.update("user", values, null, null);
	if(i==0)return false;
	else return true;
}
public User getUser(int id){
	Cursor c=db.query("user", allColumns, "id=?", new String[]{id+""}, null, null, null);
	User user=null;
	c.moveToFirst();
	while (!c.isAfterLast()){
		user=new User();
		user.setId(c.getInt(0));
		user.setEmail(c.getString(1));
		user.setPhone(c.getString(2));
		user.setAddress(c.getString(3));
		user.setSex(c.getString(4));
		user.setAccessToken(c.getString(5));
		user.setInUse(c.getInt(6));
		user.setAvatar(c.getString(7));
		c.moveToNext();
	}
	c.close();
	return user;
	}
public User getUser(String email){
	Cursor c=db.query("user", allColumns, "email=?", new String[]{email}, null, null, null);
	c.moveToFirst();
	User user=new User();
		user.setId(c.getInt(0));
		user.setEmail(c.getString(1));
		user.setPhone(c.getString(2));
		user.setAddress(c.getString(3));
		user.setSex(c.getString(4));
		user.setAccessToken(c.getString(5));
		user.setInUse(c.getInt(6));
		user.setAvatar(c.getString(7));
	c.close();
	return user;
	}
public User getUserInUse(){
	Cursor c=db.query("user", allColumns, "inuse=1", null, null, null, null);
	User user=null;
	c.moveToFirst();
	while (!c.isAfterLast()){
		user=new User();
		user.setId(c.getInt(0));
		user.setEmail(c.getString(1));
		user.setPhone(c.getString(2));
		user.setAddress(c.getString(3));
		user.setSex(c.getString(4));
		user.setAccessToken(c.getString(5));
		user.setInUse(c.getInt(6));
		user.setAvatar(c.getString(7));
		c.moveToNext();
		}
	c.close();
	return user;
	}
}
